package com.cjj.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cjj.common.convention.result.Result;
import com.cjj.common.convention.result.Results;
import com.cjj.dao.entity.ServiceOrder;
import com.cjj.dto.req.*;
import com.cjj.service.ServiceOrderService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2025-10-25
 */
@RestController
@RequestMapping("/api/serviceOrder")
@RequiredArgsConstructor
public class ServiceOrderController {

    private final ServiceOrderService serviceOrderService;

    /**
     * 分页查询当前用户订单
     * @return Result<IPage<ServiceOrder>>
     */
    @GetMapping("/getOrder")
    public Result<IPage<ServiceOrder>> getOrder(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody GetOrderReqDTO getOrderReqDTO) {
        IPage<ServiceOrder> iPage = serviceOrderService.getOrder(pageNum, pageSize, getOrderReqDTO);
        return Results.success(iPage);
    }


    /**
     * 分页查询所有待审核的订单
     * @param pageNum 页数
     * @param pageSize 页面大小
     * @return
     */
    @GetMapping("/getPending")
    public Result<IPage<ServiceOrder>> getPendingReviewOrders(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        IPage<ServiceOrder> page = serviceOrderService.getPendingReviewOrders(pageNum, pageSize);
        return Results.success(page);
    }

    /**
     * 审核通过订单
     */
    @PutMapping("/{orderId}/approve")
    public Result<Void> approveOrder(@PathVariable Long orderId) {
        serviceOrderService.approveOrder(orderId);
        return Results.success();
    }

    /**
     * 审核拒绝订单
     */
    @PutMapping("/reject")
    public Result<Void> rejectOrder(@RequestBody RejectOrderReqDTO rejectOrderReqDTO) {
        serviceOrderService.rejectOrder(rejectOrderReqDTO);
        return Results.success();
    }


    /**
     * 批量审核订单
     */
    @PutMapping("/batchReview")
    public Result<Void> batchReviewOrders(@RequestBody BatchApproveReqDTO batchApproveReqDTO) {
        serviceOrderService.batchApproveOrders(batchApproveReqDTO);
        return Results.success();
    }


    /**
     * 批量审核拒绝订单
     */
    @PutMapping("/batchReject")
    public Result<Void> batchRejectOrders(@Valid @RequestBody BatchRejectReqDTO batchRejectReqDTO) {
        serviceOrderService.batchRejectOrders(batchRejectReqDTO);
        return Results.success();
    }

    /**
     * 用户取消订单
     */
    @PutMapping("/canceled")
    public Result<Void> canceledOrder(@RequestBody CanceledOrderReqDTO canceledOrderReqDTO){
        serviceOrderService.canceledOrder(canceledOrderReqDTO);
        return Results.success();
    }

    /**
     * 用户购买商品
     * 将购买者的id填到订单表里面
     */
    @PutMapping("/purchase")
    public Result<Void> purchaseOrder(@RequestBody PurchaseOrderReqDTO purchaseOrderReqDTO){
        serviceOrderService.purchaseOrder(purchaseOrderReqDTO);
        return Results.success();
    }

    /**
     * 分页查询当前用户购买的订单
     */
    @GetMapping("/getPurchaseOrder")
    public Result<IPage<ServiceOrder>> getPurchaseOrders(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody GetPurchaseOrderReqDto getPurchaseOrderReqDto){
        IPage<ServiceOrder> page = serviceOrderService.getPurchaseOrders(pageNum, pageSize,getPurchaseOrderReqDto);
        return Results.success(page);
    }
}
